<template>
  <div id="app" name="app">
    <el-container>
      <!-- 顶部导航栏-->
      <div class="topbar">
        <div class="nav">
          <ul>
            <!-- 登录注册按钮 -->
            <li v-if="!this.$store.getters.getUser">
              <el-button type="text" @click="login" >登录</el-button>
              <span class="sep">|</span>
              <el-button type="text" @click="register = true">注册</el-button>
            </li>
            <li v-else>
              欢迎
              <el-popover placement="top" width="180" v-model="visible">
                <p>确定退出登录吗？</p>
                <div style="text-align: right; margin: 10px 0 0">
                  <el-button size="mini" type="text" @click="visible = false">取消</el-button>
                  <el-button type="primary" size="mini" @click="logout">确定</el-button>
                </div>
                <el-button type="text" slot="reference">{{this.$store.getters.getUser.userName}}</el-button>
              </el-popover>
            </li>
            <!-- 我的订单 -->
            <li>
              <router-link @click.native="toOrder" to="">我的订单</router-link>
            </li>

            <!-- 我的收藏 -->
            <li>
              <router-link @click.native="toCollect" to="">我的收藏</router-link>
            </li>

            <!-- 购物车 -->
            <li :class="getNum > 0 ? 'shopCart-full' : 'shopCart'">
              <router-link to="" @click.native="toShoppingCart">
                <i class="el-icon-shopping-cart-full"></i> 购物车
                <span class="num">({{getNum}})</span>
              </router-link>
            </li>
          </ul>
        </div>
      </div>
      <!-- 顶部导航栏结束 -->

      <!-- 顶栏容器 -->
      <el-header>
        <el-menu
          :default-active="activeIndex"
          class="el-menu-demo"
          mode="horizontal"
          active-text-color="409eff"
          router
        >
          <div class="logo">
            <router-link to="/">
              <img style="margin-top: -3px" src="./assets/imgs/logo.png" alt />
            </router-link>
          </div>
          <el-menu-item index="/">首页</el-menu-item>
          <el-menu-item index="/goods">全部商品</el-menu-item>
          <el-menu-item index="/about">关于我们</el-menu-item>

          <div class="so">
            <el-input placeholder="请输入内容" v-model="search" @keyup.enter.native="searchClick">
              <el-button slot="append" icon="el-icon-search" @click="searchClick"></el-button>
            </el-input>
          </div>
        </el-menu>
      </el-header>
      <!-- 顶栏容器结束 -->

      <!-- 登录模块 -->
      <MyLogin></MyLogin>
      <!-- 注册模块 -->
      <MyRegister :register="register" @fromChild="isRegister"></MyRegister>

      <!-- 主要区域 -->
      <el-main>
        <keep-alive>
          <router-view :key="$route.path+$route.query"></router-view>
        </keep-alive>
      </el-main>
      <!-- 主要区域结束 -->

      <!-- 页脚开始 -->
      <el-footer>
        <div class="footer">
          <div class="footer_nav con_width clearfix">
                <ul>
                  <li><a href="javascript:;"><i class="iconfont icon-buoumaotubiao46"></i>预约维修服务</a></li>
                  <li><a href="javascript:;"><i class="iconfont icon-7"></i>7天无理由退货</a></li>
                  <li><a href="javascript:;"><i class="iconfont icon-15"></i>15天免费换货</a></li>
                  <li><a href="javascript:;"><i class="iconfont icon-liwu1"></i>满150元包邮</a></li>
                  <li><a href="javascript:;"><i class="iconfont icon-dingwei"></i>520余家售后网点</a></li>
                </ul>
          </div>

          <div class="mod_help">
            <p>
              <router-link to="/">首页</router-link>
              <span>|</span>
              <router-link to="/goods">全部商品</router-link>
              <span>|</span>
              <router-link to="/about">关于我们</router-link>
            </p>
            <p class="coty">Copyright @ 发际线与我作队</p>
          </div>
        </div>
      </el-footer>
      <!-- 页脚结束 -->
    </el-container>
  </div>
</template>

<script>

import { mapActions } from "vuex";
import { mapGetters } from "vuex";

export default {
  beforeUpdate() {
    this.activeIndex = this.$route.path;
  },
  data() {
    return {
      activeIndex: "", // 头部导航栏选中的标签
      search: "", // 搜索条件
      register: false, // 是否显示注册组件
      visible: false // 是否退出登录
    };
  },
  // 通过路由获取商品id
  activated() {
    if (this.$route.query.productID != undefined) {
      this.productID = this.$route.query.productID;
    }
  },
  created() {
    // 获取浏览器localStorage，判断用户是否已经登录
    if (localStorage.getItem("user")) {
      // 如果已经登录，设置vuex登录状态
      this.setUser(JSON.parse(localStorage.getItem("user")));
    }
  },
  computed: {
    ...mapGetters(["getUser", "getNum"])
  },
  watch: {
    // 获取vuex的登录状态
    getUser: function(val) {
      if (val === "") {
        // 用户没有登录
        this.setShoppingCart([]);
      } else {
        // 用户已经登录,获取该用户的购物车信息
        this.$axios
            .post("shoppingCart/getShoppingCart", {
              user_id: val.user_id
            },{withCredentials : true})
            .then(res => {
              if (res.data.code === "001") {
                // 001 为成功, 更新vuex购物车状态
                this.setShoppingCart(res.data.shoppingCartData);
              } else {
                // 提示失败信息
                this.notifyError(res.data.msg);
              }
            })
            .catch(err => {
              return Promise.reject(err);
            });
      }
    }
  },
  methods: {
    ...mapActions(["setUser", "setShowLogin", "setShoppingCart"]),
    login() {
      // 点击登录按钮, 通过更改vuex的showLogin值显示登录组件
      this.setShowLogin(true);
    },
    // 退出登录
    logout() {
      this.visible = false;
      this.$axios
              .post("user/logout",
                      {username:this.$store.getters.getUser.userName},
                      {withCredentials : true}
              );
      // 清空本地登录信息
      localStorage.setItem("user", "");
      // 清空vuex登录信息
      this.setUser("");
      this.notifySucceed("成功退出登录");
      this.$router.go(0);
    },
    // 接收注册子组件传过来的数据
    isRegister(val) {
      this.register = val;
    },
    // 点击搜索按钮
    searchClick() {
      if (this.search != "") {
        // 跳转到全部商品页面,并传递搜索条件
        this.$router.push({ path: "/goods", query: { search: this.search } });
        this.search = "";
      }
    },
    toCollect() {
      if (!this.$store.getters.getUser) {
        this.$store.dispatch("setShowLogin",true);
      } else {
        this.$router.push({path: '/collect'});
      }
    },
    toOrder() {
      if (!this.$store.getters.getUser) {
        this.$store.dispatch("setShowLogin",true);
      } else {
        this.$router.push({path: '/order'});
      }
    },
    toShoppingCart() {
      if (!this.$store.getters.getUser) {
        this.$store.dispatch("setShowLogin",true);
      } else {
        this.$router.push({path: '/shoppingCart'});
      }
    }
  }
};
</script>

<style>

  @import "assets/css/iconfont.css";
  /* 全局CSS */
  * {
    padding: 0;
    margin: 0;
    border: 0;
    list-style: none;
  }
  #app .el-header {
    padding: 0;
  }
  #app .el-main {
    min-height: 300px;
    padding: 20px 0;
  }
  #app .el-footer {
    padding: 0;
  }
  a,
  a:hover {
    text-decoration: none;
  }
  /* 全局CSS END */

  /* 顶部导航栏CSS */
  .topbar {
    height: 40px;
    background-color: #3d3d3d;
    margin-bottom: 20px;
  }
  .topbar .nav {
    width: 1225px;
    margin: 0 auto;
  }
  .topbar .nav ul {
    float: right;
  }
  .topbar .nav li {
    float: left;
    height: 40px;
    color: #b0b0b0;
    font-size: 14px;
    text-align: center;
    line-height: 40px;
    margin-left: 20px;
  }
  .topbar .nav .sep {
    color: #b0b0b0;
    font-size: 12px;
    margin: 0 5px;
  }
  .topbar .nav li .el-button {
    color: #b0b0b0;
  }
  .topbar .nav .el-button:hover {
    color: #fff;
  }
  .topbar .nav li a {
    color: #b0b0b0;
  }
  .topbar .nav a:hover {
    color: #fff;
  }
  .topbar .nav .shopCart {
    width: 120px;
    background: #424242;
  }
  .topbar .nav .shopCart:hover {
    background: #fff;
  }
  .topbar .nav .shopCart:hover a {
    color: #ff6700;
  }
  .topbar .nav .shopCart-full {
    width: 120px;
    background: #ff6700;
  }
  .topbar .nav .shopCart-full a {
    color: white;
  }
  /* 顶部导航栏CSS END */

  /* 顶栏容器CSS */
  .el-header .el-menu {
    max-width: 1225px;
    margin: 0 auto;
  }
  .el-header .logo {
    height: 60px;
    width: 189px;
    float: left;
    margin-right: 100px;
  }
  .el-header .so {
    margin-top: 10px;
    width: 300px;
    float: right;
  }
  /* 顶栏容器CSS END */

  /* 底栏容器CSS */
  .footer {
    width: 100%;
    text-align: center;
    background: #2f2f2f;
    padding-bottom: 20px;
  }

  .footer_nav {
    background: #ffffff;
    margin: 0 auto;
    padding: 20px 0;
    border-bottom: 1px solid #e0e0e0;
  }
  .footer_nav>ul>li {
    margin-top: -20px;
    float: left;
    width: 245px;
    text-align: center;
    border-right: 1px solid #e0e0e0;
    box-sizing: border-box;
    line-height: 25px;
  }
  .footer_nav>ul>li:last-child {
    border-right: 0;
  }
  .footer_nav>ul>li>a {
    display: inline-block;
    margin-right: -286px;
    font-size: 16px;
    color: #616161;
    transition: color .2s;
  }
  .footer_nav>ul>li>a:hover {
    color: #FF6700;
  }
  .footer_nav>ul>li>a>i {
    margin-right: 5px;
    font-size: 30px;
    line-height: 24px;
    vertical-align: -4px;
  }
  .footer .mod_help {
    text-align: center;
    color: #888888;
  }
  .footer .mod_help p {
    margin: 20px 0 16px 0;
  }

  .footer .mod_help p a {
    color: #888888;
    text-decoration: none;
  }
  .footer .mod_help p a:hover {
    color: #fff;
  }
  .footer .mod_help p span {
    padding: 0 22px;
  }
  /* 底栏容器CSS END */
</style>
